package com.lstimeline.utility.DataManager;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * A static object that handles all JDBC activities
 * @author Harry
 *
 */
// TODO Connection Pooling
public abstract class AbstractDataManager {
	private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
	private static final String DB_URL = "jdbc:mysql://localhost:3307/lstimeline?autoReconnect=true";
	private static final String DB_URL2 = "jdbc:mysql://localhost/lstimeline?autoReconnect=true";
	private static final String DB_USERNAME = "lstimeline";
	private static final String DB_PASSWORD = "lst8Pass";
	
	protected static final String ENCRYPTION_PASSWORD = "p8(DL&x3#J";
	
	public static void loadDriver() {
		try {
			Class.forName(JDBC_DRIVER);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	protected static Connection getConnection() {
		Connection conn = null;
		try {
			conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
		} catch (SQLException e) {
			try {
				conn = DriverManager.getConnection(DB_URL2, DB_USERNAME, DB_PASSWORD);
			} catch(SQLException e2) {
				System.out.println("Unable to open the connection");
				e2.printStackTrace();
			}
		}
		return conn;
	}
	
	protected static void closeConnection(Connection conn) {
		if(conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				System.out.println("Unable to close the connection");
				e.printStackTrace();
			}
		}
	}
}
